(function (lib, config) {
    var loadFunc = function () {
        config.imglink = config.imglink || 'http://nkm.6660333.cn/images/nk_top.gif';
        config.bgcolor = config.bgcolor || '#333';
        function getScrollTop() {
            var scrollTop = 0;
            if (document.documentElement && document.documentElement.scrollTop) {
                scrollTop = document.documentElement.scrollTop;
            }
            else if (document.body) {
                scrollTop = document.body.scrollTop;
            }
            return scrollTop;
        }
        function getClientHeight() {
            var clientHeight = 0;
            if (document.body.clientHeight && document.documentElement.clientHeight) {
                var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
            }
            else {
                var clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
            }
            return clientHeight;
        }

        var _ua = navigator.userAgent, _ua_text = false, _position = "fixed";
        if (_ua.indexOf('baiduboxapp') > -1) {
            _ua_text = true; _position = "absolute";
        }
        var htmlStr = [
            "<style>",
            ".nk_top{ width:100%;background:{{bgcolor}}; position:" + _position + "; top:0; z-index:99;display: none;overflow: visible;}",
            ".nk_top>a{ display:block;}",
            "</style>",
            "<div class=\'nk_top\'> <a href=\'{{weblink}}\'><img width='100%' src=\'{{imglink}}\'> </a> </div>"
        ].join('');
        htmlStr = lib.util.resolveTemplate(htmlStr, 'weblink', config.weblink, 'imglink', config.imglink,'bgcolor',config.bgcolor);
        document.body.innerHTML += htmlStr;
        //window.scrollTo(0,0)
        function initShow() {
            lib.util.addEvent(window, 'scroll', function (e) {
                if (document.body.scrollTop > 0) {
                    document.getElementsByClassName('nk_top')[0].style.display = 'block';
                } else {
                    document.getElementsByClassName('nk_top')[0].style.display = 'none';
                }
            });
        }
        initShow();
        if (_ua_text) {
            var _t = document.querySelector('.nk_top').style.top;
            if (_t == '') { _t = 0; }
            lib.util.addEvent(window, 'scroll', function () {
                document.querySelector('.nk_top').style.top = parseInt(_t) + (getScrollTop() / getClientHeight()) * 100 + '%';
            });

        };
    }
    lib.load(loadFunc);
})('{{lib}}', '{{config}}');
